#include <utility>
#include "constant.h"
#include "checkerboard.h"
#include "function.h"

int rate(int sum1, int sum2, int sum3, int sum4, bool p1, bool p2, bool p3, bool p4) {
	auto Min = [] (int x, int y) -> int {return x < y ? x : y;};
	int res = INF;
	if(sum1 + sum2 == 4) res = Min(res, 1);
	else if(sum1 + sum2 == 3)
	{
		if(p1 || p2) res = Min(res, 7);
		else res = Min(res, 3);
	}
	else if(sum1 + sum2 == 2)
	{
		if(p1 || p2) res = Min(res, 11);
		else res = Min(res, 10);
	}
	else if(sum1 + sum2 == 1) res = Min(res, 14);
	
	if(sum3 + sum4 == 4) res = Min(res, 2);
	else if(sum3 + sum4 == 3)
	{
		if(p3 || p4) res = Min(res, 8);
		else res = Min(res, 5);
	}
	else if(sum3 + sum4 == 2)
	{
		if(p3 || p4) res = Min(res, 12);
		else res = Min(res, 9);
	}
	else if(sum3 + sum4 == 1) res = Min(res, 13);
	return res;
}
std::pair <int, int> AI_coordinate(Board board) {
//	int mp[20][20];
	int res = INF, tmp = INF, dx, dy;
	auto AI_check1 = [board] (int x, int y) mutable {
		int sum1 = 0; bool p1 = 0;
		for(int i = x - 1; i >= 1; --i)
		{
			if(board.Get_mp(i, y) != 0) {if(board.Get_mp(i, y) == 1) p1 = 1; break;}
			++sum1;
		}
		int sum2 = 0; bool p2 = 0;
		for(int i = x + 1; i <= 15; ++i)
		{
			if(board.Get_mp(i, y) != 0) {if(board.Get_mp(i, y) == 1) p2 = 1; break;}
			++sum2;
		}
		int sum3 = 0; bool p3 = 0;
		for(int i = x - 1; i >= 1; --i)
		{
			if(board.Get_mp(i, y) != 1) {if(board.Get_mp(i, y) == 0) p3 = 1; break;}
			++sum3;
		}
		int sum4 = 0; bool p4 = 0;
		for(int i = x + 1; i <= 15; ++i)
		{
			if(board.Get_mp(i, y) != 1) {if(board.Get_mp(i, y) == 0) p4 = 1; break;}
			++sum4;
		}
		return rate(sum1, sum2, sum3, sum4, p1, p2, p3, p4);
	};
	auto AI_check2 = [board] (int x, int y) mutable {
		int sum1 = 0; bool p1 = 0;
		for(int i = y - 1; i >= 1; --i)
		{
			if(board.Get_mp(x, i) != 0) {if(board.Get_mp(x, i) == 1) p1 = 1; break;}
			++sum1;
		}
		int sum2 = 0; bool p2 = 0;
		for(int i = y + 1; i <= 15; ++i)
		{
			if(board.Get_mp(x, i) != 0) {if(board.Get_mp(x, i) == 1) p2 = 1; break;}
			++sum2;
		}
		int sum3 = 0; bool p3 = 0;
		for(int i = y - 1; i >= 1; --i)
		{
			if(board.Get_mp(x, i) != 1) {if(board.Get_mp(x, i) == 0) p3 = 1; break;}
			++sum3;
		}
		int sum4 = 0; bool p4 = 0;
		for(int i = y + 1; i <= 15; ++i)
		{
			if(board.Get_mp(x, i) != 1) {if(board.Get_mp(x, i) == 0) p4 = 1; break;}
			++sum4;
		}
		return rate(sum1, sum2, sum3, sum4, p1, p2, p3, p4);
	};
	auto AI_check3 = [board] (int x, int y) mutable {
		int sum1 = 0; bool p1 = 0;
		for(int i = x - 1, j = y - 1; i >= 1 && j >= 1; --i, --j)
		{
			if(board.Get_mp(i, j) != 0) {if(board.Get_mp(i, j) == 1) p1 = 1; break;}
			++sum1;
		}
		int sum2 = 0; bool p2 = 0;
		for(int i = x + 1, j = y + 1; i <= 15 && j <= 15; ++i, ++j)
		{
			if(board.Get_mp(i, j) != 0) {if(board.Get_mp(i, j) == 1) p2 = 1; break;}
			++sum2;
		}
		int sum3 = 0; bool p3 = 0;
		for(int i = x - 1, j = y - 1; i >= 1 && j >= 1; --i, --j)
		{
			if(board.Get_mp(i, j) != 1) {if(board.Get_mp(i, j) == 0) p3 = 1; break;}
			++sum3;
		}
		int sum4 = 0; bool p4 = 0;
		for(int i = x + 1, j = y + 1; i <= 15 && j <= 15; ++i, ++j)
		{
			if(board.Get_mp(i, j) != 1) {if(board.Get_mp(i, j) == 0) p4 = 1; break;}
			++sum4;
		}
		return rate(sum1, sum2, sum3, sum4, p1, p2, p3, p4);
	};
	auto AI_check4 = [board] (int x, int y) mutable {
		int sum1 = 0; bool p1 = 0;
		for(int i = x - 1, j = y + 1; i >= 1 && j <= 15; --i, ++j)
		{
			if(board.Get_mp(i, j) != 0) {if(board.Get_mp(i, j) == 1) p1 = 1; break;}
			++sum1;
		}
		int sum2 = 0; bool p2 = 0;
		for(int i = x + 1, j = y - 1; i <= 15 && j >= 1; ++i, --j)
		{
			if(board.Get_mp(i, j) != 0) {if(board.Get_mp(i, j) == 1) p2 = 1; break;}
			++sum2;
		}
		int sum3 = 0; bool p3 = 0;
		for(int i = x - 1, j = y + 1; i >= 1 && j <= 15; --i, ++j)
		{
			if(board.Get_mp(i, j) != 1) {if(board.Get_mp(i, j) == 0) p3 = 1; break;}
			++sum3;
		}
		int sum4 = 0; bool p4 = 0;
		for(int i = x + 1, j = y - 1; i <= 15 && j >= 1; ++i, --j)
		{
			if(board.Get_mp(i, j) != 1) {if(board.Get_mp(i, j) == 0) p4 = 1; break;}
			++sum4;
		}
		return rate(sum1, sum2, sum3, sum4, p1, p2, p3, p4);
	};
	for(int i = 1; i <= 15; ++i) for(int j = 1; j <= 15; ++j) if(board.Get_mp(i, j) == -1)
	{
		tmp = AI_check1(i, j);
		if(tmp < res) res = tmp, dx = i, dy = j;
		tmp = AI_check2(i, j);
		if(tmp < res) res = tmp, dx = i, dy = j;
		tmp = AI_check3(i, j);
		if(tmp < res) res = tmp, dx = i, dy = j;
		tmp = AI_check4(i, j);
		if(tmp < res) res = tmp, dx = i, dy = j;
	}
	return std::make_pair(dx, dy);
}
